<?php
/*
 * Created on Feb 5, 2009
 * @author: ANH
 * @PARAM rtype is rootType: example: caves is type then attraction is rtype
 * @filesource: modules/place/controllers/PlaceController.php
 */

class Admin_MasterController extends Clu_Controller_Action_Master
{
	public function init()
	{
		$this->m = new admin_Model_Weight();
		$this->mPlace = new place_Model_Place();
		$this->mEvent = new event_Model_Service();
		$this->mNodeService = new node_Model_NodeService();
		$this->mService = new admin_Model_Service();
		$this->mAds = new ads_Model_Ads();
		parent::init();
	}
	
	/*
	 * expect 4 params after "Search" is clicked: 
	 * 		theme
	 * 		site
	 * 		content_module 
	 * 		type  (if content_module == "node")
	 * 		request
	 * 		widget_position_id (the KEY in widget_positions table)
	 */
	public function manageFeaturedContentAction()
	{
		
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, "position widget") === false)
		{
			$this->renderError(null, t(414));
			return;
		}
		$this->viewParams['form'] = new admin_Form_Admin();
        $actionUrl = $this->view->hvnUrl(array('module' => 'admin', 'action'=>'manage-featured-content'), 'nocontroller');
        $this->viewParams['form']->cluBuild(array('action_url' => $actionUrl, 'userPermissions' => $sess->userPermissions));
		$data = $this->_request->getParams();
		//$this->viewParams['form']->populateFormData($data);
		if ($this->_request->isPost() || isset($data['cluSearch']))
		{
			$dataSearch = array(/*'status' => 'featured',*/ 'has_thumb' => 1);
			if (!isset($data['themes']) || !isset($data['content_module']) 
				|| !isset($data['request']) || !isset($data['widget_position_id'])
			)
			{
				return;
			}
			$this->viewParams['themes'] = $data['themes'];
			
			$this->viewParams['type'] = $data['type'];
			$this->viewParams['request'] = $data['request'];
			$this->viewParams['widget_position_id'] = $data['widget_position_id'];
			
			//now that we know which specific widget (& position on which template, master_request)
			//we need to get which content Module to list
			$widgetInfo = $this->m->getWidgetInfo($data['widget_position_id']);
			$this->viewParams['widgetInfo'] = $widgetInfo[0];
			$tmp = explode ("/",$widgetInfo[0]['widget_request']);
			$widgetModule = $tmp[0];  //module name (event, place, node..)
			//$this->viewParams['content_module'] = $data['content_module'];
			$this->viewParams['content_module'] = $widgetModule;
			//get the featured content
			if($widgetModule == 'place')
			{
				$results = $this->mPlace->search($dataSearch);
			}
			else if($widgetModule  == 'event')
			{
				$results = $this->mEvent->search($dataSearch);
			}
			else if($widgetModule == 'node' || $widgetModule == 'gallery')
			{
				$dataSearch = array(/*'status' => 1 , 'has_thumb' => 1*/);
				$nodeType = $data['type'];
				//$nodeType = $this->viewParams['widgetInfo']['node_type'];
				$this->viewParams['type'] = $nodeType;
				$nodeTypeInfo = $this->mNodeService->getNodeTypeInfo($nodeType);
				$results = $this->mNodeService->search($dataSearch, $nodeTypeInfo);
			}
			else if($widgetModule == 'ads')
			{
				$results = $this->mAds->search(null);
			}
			/*TODO: use paging here. Don't fetch all*/
			$this->viewParams['featuredItems'] = $this->db->fetchAll($results);
			//get the already-selected- items
			//  "select content_id from `widget_item_weights` where
			//		 widget_position_id=$data['widget_position_id'] ";
			//$data now contains 'widget_position_id' field
			//echo $data['widget_position_id'];
			$currentItems = $this->m->searchCurrentItemsByWidgetPositionId($data);
			$this->viewParams['currentItems'] = $currentItems;
			$currentItemIds = array();
			foreach ($currentItems as $item)
				$currentItemIds[] = $item['content_id'];
			$this->viewParams['currentItemIds'] = $currentItemIds;
		}		
	}
	/*
	 * Use tableDnD to re-order the weights of featured items displayed 
	 * in home page or any page
	 * 
	 * Expecting 2 params
	 * 	$content_module: 'event' or 'place',.
	 * widget_position_id: which widget in which page, for example 'home'
	 * ('event', 'home') would identify the list of featured-events displayed in home
	 */
    public function orderWidgetItemsWeightsAction()
    {
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, "position widget") === false)
		{
			$this->renderError(null, t(414));
			return;
		}
		
		if(!($data['content_module'] = $this->_request->getParam('content_module')) )//|| 
		   //!($data['widget_position_id'] = $this->_request->getParam('widget_position_id')))
		{
			$this->presenceIdentifier = 2;
			return;
		}
		
		$rows = $this->m->search($data);
		$this->viewParams['weights'] = $rows;
    }
    
    public function updateNumberOfPlacesByCityAction()
    {
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos ($sess->userPermissions, "administer everything") === false)
		{
			$this->renderError(null, t(414));
			return;
		}
    	$this->mService->updateNumberOfPlacesInCityAndDistrictByCategory();
//    	$this->mService->updateNumberOfPlaces();
    }
    
}
?>
